package com.ruoyi.api.controller.moon;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.MoonUser;
import com.ruoyi.common.core.domain.vo.UserInformationVO;
import com.ruoyi.moon.domain.MoonNotice;
import com.ruoyi.moon.domain.vo.MyReplyVO;
import com.ruoyi.moon.domain.vo.MyReviewsVO;
import com.ruoyi.moon.service.IMoonNoticeService;
import com.ruoyi.moon.service.IMoonReplyService;
import com.ruoyi.moon.service.IMoonReviewsService;
import com.ruoyi.moon.service.IMoonUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/moon/my")
public class ApiMoonMyController extends BaseController {

    @Autowired
    private IMoonUserService moonUserService;
    @Autowired
    private IMoonReviewsService moonReviewsService;
    @Autowired
    private IMoonReplyService moonReplyService;
    @Autowired
    private IMoonNoticeService moonNoticeService;


    /**
     * 获取个人信息
     */
    @GetMapping(value = "/personal")
    public AjaxResult personalInformation()
    {
        UserInformationVO userInformationVO = moonUserService.selectMoonUserInformation(getUserId());
        return success(userInformationVO);
    }


    /**
     * 我的评价
     */
    @GetMapping(value = "/evaluate")
    public AjaxResult getRestore()
    {
        List<MyReviewsVO> myReviews = moonReviewsService.selectMoonEvaluate(getUserId());
        return success(myReviews);
    }


    /**
     * 我的回复
     */
    @GetMapping(value = "/restore")
    public AjaxResult getEvaluate()
    {
        List<MyReplyVO> myReply = moonReplyService.selectMoonRestore(getUserId());
        return success(myReply);
    }

    /**
     * 修改用户信息
     */
    @RepeatSubmit
    @PostMapping("/updateInfo")
    public AjaxResult updateInfo(@RequestBody MoonUser moonUser)
    {
        moonUser.setId(getUserId());
        return toAjax(moonUserService.updateInfo(moonUser));
    }

    /**
     * 未读消息
     * @return
     */
    @GetMapping("/getMyUnNotice")
    public AjaxResult getMyUnNotice(){
        MoonNotice notice = new MoonNotice();
        notice.setUserId(getUserId());
        notice.setNoticeState(0);
        List<MoonNotice> notices = moonNoticeService.selectMoonNoticeList(notice);
        return AjaxResult.success(notices.size());
    }

    /**
     * 我的消息
     * @return
     */
    @GetMapping("/getMyNotice")
    public AjaxResult getMyNotice(){
        MoonNotice notice = new MoonNotice();
        notice.setUserId(getUserId());
        List<MoonNotice> notices = moonNoticeService.selectMoonNoticeList(notice);
        return AjaxResult.success(notices);
    }

    /**
     * 消息详情
     * @param id
     * @return
     */
    @GetMapping("/getNoticeInfo")
    public AjaxResult getNoticeInfo(Long id){
        MoonNotice notice = moonNoticeService.selectMoonNoticeById(id);
        if(null != notice){
            if(!notice.getUserId().toString().equals(getUserId().toString())){
                return AjaxResult.error("???");
            }
            if(notice.getNoticeState() < 1){
                notice.setNoticeState(1);
                moonNoticeService.updateMoonNotice(notice);
            }
        }

        return AjaxResult.success(notice);
    }



}
